package com.hitqz.robot.biz.flow.component.robotEquipmentSample;

import com.alibaba.fastjson.JSONObject;
import com.hitqz.robot.api.business.enums.TicketsFormFieldEnum;
import com.hitqz.robot.api.business.model.dto.FlowContextDto;
import com.hitqz.robot.api.business.model.dto.FlowPatrolPreAndEndTicketsDto;
import com.hitqz.robot.api.common.entity.RobotEquipment;
import com.hitqz.robot.api.common.entity.TicketsTask;
import com.hitqz.robot.biz.service.RobotEquipmentService;
import com.hitqz.robot.camera.EquipmentFactory;
import com.hitqz.robot.camera.IEquipmentService;
import com.hitqz.robot.camera.configure.EquipmentCmdEnum;
import com.hitqz.robot.camera.dto.DoorCtrlDto;
import com.hitqz.robot.camera.dto.QueryParamsDto;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.core.NodeComponent;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author xupkun
 * @date 2024/10/4
 */
@LiteflowComponent(value = "CloseOilCabinetComponent", name = "[取油样机器人]关闭柜门")
@RequiredArgsConstructor
@Slf4j
public class CloseOilCabinetComponent extends NodeComponent {

    private final RobotEquipmentService robotEquipmentService;

    private final EquipmentFactory equipmentFactory;

    @Override
    public void process() throws Exception {
        FlowContextDto dto =  this.getRequestData();
        TicketsTask task = dto.getTicketsTask();
        log.info("任务数据 {}", task.getParseOrderParams());
        FlowPatrolPreAndEndTicketsDto ticketsDto = JSONObject.parseObject(dto.getTicketsTask()
                .getParseOrderParams(), FlowPatrolPreAndEndTicketsDto.class);
        String deviceKey = "";
        for (String key : ticketsDto.getModalData().keySet()) {
            if (key.contains(TicketsFormFieldEnum.EQUIPMENT_LIST.getFieldName())) {
                deviceKey = key;
                break;
            }
        }
        String device = String.valueOf(ticketsDto.getModalData().get(deviceKey));
        RobotEquipment robotEquipment = robotEquipmentService.getById(Integer.valueOf(device));
        IEquipmentService service =  equipmentFactory.getServiceByBrand(robotEquipment.getBrand());
        QueryParamsDto queryParamsDto = new QueryParamsDto();
        queryParamsDto.setPassword(robotEquipment.getPassword());
        queryParamsDto.setUsername(robotEquipment.getUsername());
        queryParamsDto.setPort(robotEquipment.getPort());
        queryParamsDto.setChannel(robotEquipment.getChannel());
        queryParamsDto.setIp(robotEquipment.getIp());
        DoorCtrlDto doorCtrlDto = new DoorCtrlDto();
        doorCtrlDto.setDoor(2);
        service.executeCmd(queryParamsDto , false , EquipmentCmdEnum.PLC_DOOR_CTRL, doorCtrlDto);
    }

}
